<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFinanceTransferRecordsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('finance_transfer_records', function (Blueprint $table) {
            $table->id();
            $table->char('order_id', 32)->unique()->comment('订单号');
            $table->tinyInteger('status')->default(1)->comment('状态; 1:转账中 2:已完成 3:转账失败');
            $table->char('reason_for_failure')->default('')->comment('失败原因');

            $table->tinyInteger('target_type')->default(1)->comment('目标类型; 1:对公账号 2:供应商');
            $table->unsignedBigInteger('subject_id')->default(0)->comment('所属模型 ID');
            $table->char('subject_type', 64)->default('')->comment('所属模型');

            $table->unsignedBigInteger('amount')->comment('转账涉及金额 单位分');

            $table->unsignedBigInteger('balance')->default(0)->comment('结算后 所剩现金余额(cashBal) 单位分');
            $table->json('balance_snapshot')->nullable()->comment('所剩余额快照');

            $table->timestamps();

            $table->index(['subject_id', 'subject_type']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('finance_transfer_records');
    }
}
